<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();
    public $param = "muah";

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();

    public function filterHttps($filterChain) {
        $filter = new HttpsFilter;
        $filter->filter($filterChain);
    }

    private function VisitorIP() {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $TheIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else
            $TheIp = $_SERVER['REMOTE_ADDR'];

        return trim($TheIp);
    }

    public function inputIp() {
        $model = new COUNTER;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);




        $cekIP = $this->cekIp($this->VisitorIP());      

        if (count($cekIP) > 0) {
            $dateAndTime = explode(" ", $cekIP->created_at);
            $date = explode("-", $dateAndTime[0]);
            $time = explode(":", $dateAndTime[1]);
            $dateOneDayAdded = mktime($time[0], $time[1], $time[2], $date[1], $date[2] + 1, $date[0]);
            if (date("Y-m-d H:i:s") > date("Y-m-d H:i:s", $dateOneDayAdded)) {
                $model->ip = $this->VisitorIP();
                $model->save();
            }
        } else if (count($cekIP) == 0) {

            $model->ip = $this->VisitorIP();
            $model->save();
        }
        echo count($this->counter());
    }

    private function cekIp($ip) {
        $criteria = new CDbCriteria;
        $criteria->select = "created_at";
        $criteria->condition = "ip=:ip";
        $criteria->params = array(":ip" => $ip);
        $criteria->order = "created_at DESC";
        $model = COUNTER::model()->find($criteria);
        return $model;
    }
    
    private function counter(){
        $criteria = new CDbCriteria;
        $criteria->select = "id_counter";     
        $model = COUNTER::model()->findAll($criteria);
        return $model; 
    }

}